package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.exception.DeleteException;
import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Slf4j
@RequestMapping("/depts")
@RestController//@Controller+@ResponseBody
public class DeptController {
    @Autowired
    private DeptService deptService;


    @GetMapping
    public Result list(){
        //System.out.println("查询全部的部门数据");
        log.info("查询全部的部门数据");
        List<Dept> deptlist = deptService.findAll();
        return Result.success(deptlist);
    }
    @Log
    @DeleteMapping
    public Result delete(Integer id)  {
        //System.out.println("删除部门id为："+id);
        log.info("删除部门id为："+id);
        deptService.deleteById(id);
        return Result.success();
    }
    @Log
    @PostMapping
    public Result add(@RequestBody Dept dept){
        //System.out.println("添加部门："+dept);
        log.info("添加部门："+dept);
        deptService.add(dept);
        return Result.success();
    }
    @Log
    @PutMapping
    public Result update(@RequestBody Dept dept){
        //System.out.println("修改部门："+dept);
        log.info("修改部门："+dept);
        deptService.update(dept);
        return Result.success();
    }
    @GetMapping("/{id}")
    public Result findbyId(@PathVariable Integer id){
        //System.out.println("查询部门id为："+id);
        log.info("查询部门id为："+id);
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

}
